<?php
require_once( '../../libs/smarty.inc' );
require_once( '../../libs/smt_variables.php' );
require_once( '../../libs/Class_DB.php' );
require_once( '../../libs/Class_ERROR.php' );
require_once( '../../libs/login.inc.php' );



if( !in_array( "M", $_SESSION['ADMIN_LOGIN']['AUTH_CD'] ) ) exit;

if( $_SERVER['REQUEST_METHOD'] == 'GET' ){

	if( isset($_GET['shop_id']) && $_GET['shop_id'] != '' ){	
			
		$db = new Class_DB;
		
		if($_GET['mode'] == 'update'){
			
			//SQLクエリー
			$sql = "SELECT * FROM p_shop where shop_id =".$_GET['shop_id'];	
			
			$res = $db->query($sql);
			
			$list = array("shop_id"=>$shop_id);
			
			$cnt = 0;
				
			while( $row = mysql_fetch_array($res,MYSQL_ASSOC) ){	
				$row['id']=sprintf('%04d', $row['shop_id']);
				$row['u_date']=$row['u_date']==NULL?"":date('Y/m/d',strtotime($row['u_date']));
				$row['r_date']=$row['r_date']==NULL?"":date('Y/m/d',strtotime($row['r_date']));
				//$sarray = unserialize($row['style']);
				if($row['style']==''){
					$row['u_style1'] = $row['style1'];
					$row['u_style2'] = $row['style2'];
					$row['style1'] = $row['style'];
					$row['sty'] = 2;
				}else{
					$row['style1'] = $row['style'];
					$row['sty'] = 1;
				}
				$consumerlist[] = $row;				
				$cnt++;
			}		
			
			$smarty->assign('v', $consumerlist[0]);
			$smarty->assign('cnt', $cnt);
	
		}elseif ($_GET['mode'] == 'delete'){
			//删除
			//SQLクエリー
			$sql = "DELETE FROM p_shop WHERE shop_id =".$_GET['shop_id'];
			
			$db->query( $sql );	
			
			$phase = 'completedelete';
			$smarty->assign('phase', $phase);
			$smarty->assign('cnt', 1);
		}
	}
}


//登録
if($_SERVER['REQUEST_METHOD'] == 'POST'){
	
	$db = new Class_DB;
	
	if($_POST['mode']=="input"){
		
		$err = new Class_ERROR;
		$err_mes['type'] = $err->check( $_POST['type'], array("EXIST") );
		$err_mes['shop_name'] = $err->check( $_POST['shop_name'], array("EXIST") );
		if($_POST['sty']==1){
			$err_mes['style1'] = $err->check( $_POST['style1'], array("EXIST") );
		}else{
			$err_mes['u_style1'] = $err->check( $_POST['u_style1'], array("EXIST","NUM") );
			$err_mes['u_style1'] = $err->check( $_POST['u_style2'], array("EXIST","NUM") );
		}
		$err_mes['refer_price'] = $err->check( $_POST['refer_price'], array("EXIST","NUM") );
		$err_mes['cost_price'] = $err->check( $_POST['cost_price'], array("EXIST","NUM") );
		$err_mes['applicant'] = $err->check( $_POST['applicant'], array("EXIST") );
		
		if( $err->clear ){

			$type = $_POST['type'];
			$shop_name = $_POST['shop_name'];
			$style = $_POST['style1'];
			$u_style1 = $_POST['u_style1'];
			$u_style2 = $_POST['u_style2'];
			$refer_price = $_POST['refer_price'];
			$cost_price = $_POST['cost_price'];
			$s_unit = $_POST['s_unit'];
			$applicant = $_POST['applicant'];
			$remark = $_POST['remark'];			
			
			$date = date('Y/m/d G:i:s');
			
			//SQLクエリー
			$sql = "INSERT INTO p_shop(type,shop_name,style,style1,style2,refer_price,cost_price,s_unit,r_date,u_date,applicant,remark) VALUES('".$type."','".$shop_name."'";
			$sql = $sql.",'".$style."'";
			
			if($_POST['sty']==1){
				$sql = $sql.",''";
				$sql = $sql.",''";
			}else {
				$sql = $sql.",'".$u_style1."'";
				$sql = $sql.",'".$u_style2."'";
			}
			
			
			$sql = $sql.",'".$refer_price."','".$cost_price."','".$s_unit."','".$date."','".$date."','".$applicant."','".$remark."')";	

			$db->query( $sql );
			
			$phase = 'complete';
			$smarty->assign('phase', $phase);
			
		}else{
			
			$smarty->assign('err', $err_mes);
			$smarty->assign('forms', $_POST);
			
		}
		
	}elseif ($_POST['mode']=="update"){
			$err = new Class_ERROR;
			$err_mes['type'] = $err->check( $_POST['type'], array("EXIST") );
			$err_mes['shop_name'] = $err->check( $_POST['shop_name'], array("EXIST") );
			if($_POST['sty']==1){
				$err_mes['style1'] = $err->check( $_POST['style1'], array("EXIST") );
			}else{
				$err_mes['u_style1'] = $err->check( $_POST['u_style1'], array("EXIST","NUM") );
				$err_mes['u_style1'] = $err->check( $_POST['u_style2'], array("EXIST","NUM") );
			}
			$err_mes['refer_price'] = $err->check( $_POST['refer_price'], array("EXIST","NUM") );
			$err_mes['cost_price'] = $err->check( $_POST['cost_price'], array("EXIST","NUM") );
			$err_mes['applicant'] = $err->check( $_POST['applicant'], array("EXIST") );
			
			if($err->clear){
				//更新
				$shop_id = $_POST['shop_id'];
				$r_date = $_POST['r_date'];
				$type = $_POST['type'];
				$shop_name = $_POST['shop_name'];
				$style = $_POST['style1'];
				$u_style3 = $_POST['u_style1'];
				$u_style4 = $_POST['u_style2'];
				$refer_price = $_POST['refer_price'];
				$cost_price = $_POST['cost_price'];
				$s_unit = $_POST['s_unit'];
				$applicant = $_POST['applicant'];
				$remark = $_POST['remark'];		
				
				
				$date = date('Y/m/d G:i:s');
				
				$sql = "UPDATE p_shop SET ";
				
				if($type != '' || $type != null){
					$sql = $sql." type='".$type."',";
				}
				
				if($shop_name != '' || $shop_name != null){
					$sql = $sql."shop_name='".$shop_name."',";
				}
				
				
				if($_POST['sty']==1){
					$sql = $sql."style='$style',";
					$sql = $sql."style1='',";
					$sql = $sql."style2='',";
				}else {
					$sql = $sql."style='',";
					$sql = $sql."style1='".$u_style3."',";
					$sql = $sql."style2='".$u_style4."',";
				}
				
				/*
				if($_POST['sty']==2){
					$sarray = array($u_style3,$u_style4);
					$u_style = $u_style3."mm×".$u_style4."m";
					$sql = $sql."style='".serialize($sarray)."',";
				}else{
					$sarray = array($style);
					$sql = $sql."style='".serialize($sarray)."',";
				}
				*/
				
				if($refer_price != '' || $refer_price != null){
					$sql = $sql."refer_price='".$refer_price."',";
				}
				
				if($cost_price != '' || $cost_price != null){
					$sql = $sql."cost_price='".$cost_price."',";
				}
				
				if($s_unit != '' || $s_unit != null){
					$sql = $sql."s_unit='".$s_unit."',";
				}
				
				if($applicant != '' || $applicant != null){
					$sql = $sql."applicant='".$applicant."',";
				}
				
				if($remark != '' || $remark != null){
					$sql = $sql."remark='".$remark."',";
				}
				
				$sql = $sql." u_date='".$date."' WHERE shop_id='".$shop_id."'";
				
				$db->query( $sql );
			
				$phase = 'completeupdate';
				$smarty->assign('phase', $phase);
				$smarty->assign('cnt', 1);
		}else{
				$smarty->assign('err', $err_mes);
				$smarty->assign('v', $_POST);
				$smarty->assign('cnt', 1);
			
		}
		
	}else{
		header("Location:./shop_index.php");
	}
			
}


$menu_html = file_get_contents( $_SERVER['DOCUMENT_ROOT'].'/templates/web-admin/menu.html' );

$smarty->assign('menu_html', $menu_html);

$smarty->assign('consumerlist', $consumerlist);

$smarty->assign('list', $list);

$smarty->display('web-admin/shop_form.html');


?>